﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace PSSAirbusVoiceExtender
{
  public class ApiWavPlayer : IDisposable, PSSAirbusVoiceExtender.IWavPlayer
  {
    [DllImport("WinMM.dll")]
    public static extern bool PlaySound(string fname, int Mod, int flag);     // these are the SoundFlags we are using here, check mmsystem.h for more    
    public int SND_ASYNC = 0x0001;     // play asynchronously    
    public int SND_FILENAME = 0x00020000; // use file name    
    public int SND_PURGE = 0x0040;     // purge non-static events     

    private bool isPlaying = false;

    public void Play(string fileName)
    {
      PlaySound(fileName, 0, SND_FILENAME | SND_ASYNC);
      isPlaying = true;
    }

    public void Stop()
    {
      PlaySound(null, 0, SND_PURGE);
      isPlaying = false;
    }

    ~ApiWavPlayer()
    {
      if (isPlaying)
      Stop();
    }

    public void Dispose()
    {
      if (isPlaying)
      Stop();
    }

  }
}
